/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package at.dcmspl;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.ejb.Stateful;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 * The Container Based Transaction ProcessingNodeBMT
 * @author Hannes Stornig, Gosch Daniel
 */
@Stateful
public class ProcessingNodeCBT implements ProcessingNodeCBTRemote{
    
    @Resource SessionContext sc;
    
    @Override
    public void doSomething2() throws IOException, NamingException{
        Context context = new InitialContext();
        DcmQueueProducerServiceRemote dcmQueueProducerService = (DcmQueueProducerServiceRemote) context.lookup("java:global/DcmQueueService-ejb/DcmQueueService!at.dcmspl.DcmQueueProducerServiceRemote"); 
        dcmQueueProducerService.createDcmQueue("1.Queue");
        dcmQueueProducerService.addDcmFile("1.Queue", "eins");
        dcmQueueProducerService.addDcmFile("1.Queue", "zwei");
    }

    @Override
    public void doSomething() throws IOException, NamingException{
        Logger.getLogger(ProcessingNodeBMT.class.getName()).log(Level.INFO, "###ProcessingNode Main");
        Logger.getLogger(ProcessingNodeBMT.class.getName()).log(Level.INFO, "### STARTING CLIENT with Container Based Transaction........");
        //JNDI LOOKUP
        Context context = new InitialContext();
        DcmQueueProducerServiceRemote dcmQueueProducerService = (DcmQueueProducerServiceRemote) context.lookup("java:global/DcmQueueService-ejb/DcmQueueService!at.dcmspl.DcmQueueProducerServiceRemote"); 
        DcmQueueConsumerServiceRemote dcmQueueConsumerService = (DcmQueueConsumerServiceRemote) context.lookup("java:global/DcmQueueService-ejb/DcmQueueService!at.dcmspl.DcmQueueConsumerServiceRemote"); 
        
        try{
        //dcmQueueProducerService.createDcmQueue("1.Queue");
        //dcmQueueProducerService.addDcmFile("1.Queue", "eins");
        //dcmQueueProducerService.addDcmFile("1.Queue", "zwei");
        
           
        dcmQueueProducerService.addDcmFile("1.Queue", "drei");
        DcmFile pool = dcmQueueConsumerService.poolDcmFile(dcmQueueConsumerService.findDcmQueueByName("1.Queue"));
        dcmQueueProducerService.addDcmFile("1.Queue", "vier");
        dcmQueueProducerService.addDcmFile("1.Queue", "funf");
        //DcmFile elementAt = dcmQueueConsumerService.elementAt(dcmQueueProducerService.findDcmQueueByName("1.Queue"), 1);      
        
        
        dcmQueueProducerService.createDcmQueue("2.Queue");
        dcmQueueProducerService.createDcmQueue("3.Queue");
        dcmQueueProducerService.addDcmFile(dcmQueueProducerService.findDcmQueueByName("2.Queue"), pool);
        dcmQueueProducerService.addDcmFile(dcmQueueProducerService.findDcmQueueByName("3.Queue"), pool);
        dcmQueueConsumerService.poolDcmFile(dcmQueueProducerService.findDcmQueueByName("2.Queue"));
        //dcmQueueConsumerService.poolDcmFile(dcmQueueProducerService.findDcmQueueByName("3.Queue"));
        //sc.setRollbackOnly();
        
        //Aufräumen
        dcmQueueConsumerService.remove(dcmQueueProducerService.findDcmQueueByName("3.Queue"));
        dcmQueueConsumerService.remove(dcmQueueProducerService.findDcmQueueByName("1.Queue"));
        dcmQueueConsumerService.remove(dcmQueueProducerService.findDcmQueueByName("1.Queue"));
        dcmQueueConsumerService.remove(dcmQueueProducerService.findDcmQueueByName("1.Queue"));
        dcmQueueConsumerService.remove(dcmQueueProducerService.findDcmQueueByName("1.Queue"));
        dcmQueueConsumerService.removeDcmQueue(dcmQueueProducerService.findDcmQueueByName("1.Queue"));
        dcmQueueConsumerService.removeDcmQueue(dcmQueueProducerService.findDcmQueueByName("2.Queue"));
        dcmQueueConsumerService.removeDcmQueue(dcmQueueProducerService.findDcmQueueByName("3.Queue"));
            
        }catch(Throwable t){
            try{
                sc.setRollbackOnly();
                Logger.getLogger(ProcessingNodeCBT.class.getName()).log(Level.INFO, "###Rollback: {0}",sc.getRollbackOnly());
            }catch(Exception ex){
                System.out.println("Transaktion fehlgeschlagen: " + t.getMessage());
                Logger.getLogger(ProcessingNodeBMT.class.getName()).log(Level.INFO, "Error: {0}",ex);
            }
        }
    } 
}
